package com.example.playedu;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;

@Controller
public class HelloController {
    @GetMapping("/")
    public String hello() {
        return "index";
    }

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @GetMapping("/register")
    public String register() {
        return "register";
    }

    @GetMapping("/password-reset")
    public String passwordRest() {
        return "password-reset";
    }

    @GetMapping("/videos")
    public String videos() {
        return "videos";
    }

    @GetMapping("/videos-show")
    public String videosShow() {
        return "videos-show";
    }

    @GetMapping("/videos-show-lecture")
    public String videosShowLecture() {
        return "videos-show-lecture";
    }

    @GetMapping("/docs")
    public String docs() {
        return "docs";
    }

    @GetMapping("/docs-show")
    public String docsShow() {
        return "docs-show";
    }

    @GetMapping("/posts")
    public String posts() {
        return "posts";
    }

    @GetMapping("/posts-show")
    public String postsShow() {
        return "posts-show";
    }

    @Autowired
    private JavaMailSender sender;
    @Autowired
    private Environment environment;

    @GetMapping("/send-mail-demo")
    @ResponseBody
    public String send() throws MessagingException, UnsupportedEncodingException {
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);
        helper.setFrom(new InternetAddress(environment.getProperty("spring.mail.username"), "Admin"));
        helper.setSubject("(test) Hello, world!");
        helper.setTo("要发送给谁@xxx.com");
        helper.setText("Thank you for ordering!");

        sender.send(message);

        return "ok";
    }
}
